﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NanoERP.Business.Permission;
using NanoERP.Model.Poco;
using NanoERP.Infrastructure;

namespace NanoERP.Modules.SystemMgmt
{
    
    public partial class FrmEditDepartment : EditFormBase
    {
        private SYS_Department _data;
        public SYS_Department Data
        {
            get
            {
                return _data;
            }
            set
            {
                _data = value;
                this.Mode = this.Enabled ? FormMode.Edit : FormMode.View;
            }
        }

        public Guid? ParentId { get; set; }

        public FrmEditDepartment()
        {
            InitializeComponent();
        }

        protected override void LoadData()
        {
            this.Id = Data.Id;
            //this.longinNameTextBox.Text = Data.LoginName;
            this.descriptionTextBox.Text = Data.Description;
            this.nameTextBox.Text = Data.Name;
            this.sortTextBox.Text = Data.Sort.ToString();
        }

        protected override bool SaveData()
        {
            var srv = new DepartmentService();

            SYS_Department dep = !this.Id.Equals(Guid.Empty) ? srv.Get(this.Id) : new SYS_Department()
            {
                Id = Guid.NewGuid(),
                ParentId = ParentId
            };

            //dep.LoginName = this.longinNameTextBox.Text;
            dep.Description = this.descriptionTextBox.Text;
            dep.Name = this.nameTextBox.Text;
            dep.Sort = Int32.Parse(this.sortTextBox.Text);

            if (this.Id.Equals(Guid.Empty))
            {
                srv.InsertDepartment(dep);
            }
            else
            {
                srv.UpdateDepartment(dep);
            }
            return true;
        }
        
        ////实现数据双向绑定
        //protected override void BindData(IList<BindingSettings> list)
        //{
        //    list.Add(new BindingSettings(this.nameTextBox, "Name"));
        //    list.Add(new BindingSettings(this.sortTextBox, "Sort", true, String.Empty));
        //    list.Add(new BindingSettings(this.descriptionTextBox, "Description"));
        //}

        
    }
}
